package com.FH.organization.Attachment;

import com.FH.util.AccessDB;
import com.FH.util.ResultDTO;
import com.FH.util.warnSrc;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import javax.ejb.Stateless;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/17.
 */
@Stateless
public class AttachmentDAOImpl extends AccessDB implements AttachmentDAO {
    /**
     * 查询附件信息
     * @param search
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResultDTO getAttachmentInfo(String search, String page, String size) {

        ResultDTO resultDTO = new ResultDTO();

        try {
            JSONObject json = new JSONObject(search);
            String existName = "0";

            //先查询数据库中是否存在字段
            String sqlexit = "select count(1) from attachment  where 1=1 ";

            String sql_w = "";//查询条件sql组合
            String sql_page = "";//分页组合

            Integer page_ok = Integer.parseInt(page);//将定义page、size的String类型转换成Integer类型
            Integer size_ok = Integer.parseInt(size);

            if (page.equals("1")) {
                sql_page = " limit 0," + size + "";
            } else {
                sql_page = " limit " + ((page_ok - 1) * size_ok + 1) + "," + size + "";
            }

            if (json.has("attachment_id")) {
                sql_w += " AND attachment_id = '" + json.getString("attachment_id") + "'";
            }


            String sql_ok = sqlexit + sql_w + sql_page;//组合好的sql语句
            existName = getReportingToOne(sql_ok);

            List<String[]> seresult = new ArrayList<String[]>();

            List<AttachmentShowDTO> result = new ArrayList<AttachmentShowDTO>();
            String[] image_path = this.getImage_path();
            String image_pathSerAddr = image_path[2];//图片访问地址
            if (!existName.equals("0")) {
                String sql = " select attachment_id,attachment_addr,head_portrait_time,head_portrait_type,file_type,file_name FROM attachment  where 1=1 " + sql_w + sql_page;
                //     String sql ="select * from finance_budget" ;
                seresult = this.getReportingTo(sql);

                //构建返回对象
                for (int i = 0; i < seresult.size(); i++) {

                    AttachmentShowDTO fbs = new AttachmentShowDTO();

                    fbs.setAttachment_id(seresult.get(i)[1]);

                    fbs.setAttachment_addr(image_pathSerAddr+seresult.get(i)[2]);
                    fbs.setHead_portrait_time(seresult.get(i)[3]);
                    fbs.setHead_portrait_type(seresult.get(i)[4]);
                    fbs.setFile_type(seresult.get(i)[5]);
                    fbs.setFile_name(seresult.get(i)[6]);


                    result.add(fbs);
                }
            }
            if (seresult.size() != 0) {
                resultDTO.setStatus("90000");
                resultDTO.setDescritpion(warnSrc.get_success);
            } else {
                resultDTO.setStatus("90001");
                resultDTO.setDescritpion(warnSrc.get_fail);
            }
            resultDTO.setValues(result);

        } catch (JSONException e) {
            e.printStackTrace();

        }
        return resultDTO;
    }

    @Override
    public ResultDTO addAttachmentInfo(AttachmentPullDTO attachmentPullDTO) {
        ResultDTO resultDTO = new ResultDTO();
        String file = attachmentPullDTO.getFile();
        resultDTO.setStr(file);
        resultDTO.setStatus("90000");
        resultDTO.setDescritpion("附件信息调用成功");

        return resultDTO;
    }

    @Override
    public ResultDTO deleteAttachmentInfo(AttachmentPullDTO attachmentPullDTO) {
        //删除之前查看该表是否有记录
        String sql = "select count(1) from attachment where attachment_id='" + attachmentPullDTO.getAttachment_id() + "'";
        String existName = getReportingToOne(sql);

        int result = 0;

        ResultDTO resultDTO = new ResultDTO();

        if (!existName.equals("0")) {
            String QUERY = "";
            QUERY += "delete from attachment where attachment_id='" + attachmentPullDTO.getAttachment_id() + "'";

            result = this.addReportingTo(QUERY);
        }

        if (result != 0) {
            resultDTO.setStatus("90000");
            resultDTO.setDescritpion(warnSrc.delete_success);

        } else {
            resultDTO.setStatus("90001");
            resultDTO.setDescritpion(warnSrc.delete_fail);

        }
        return resultDTO;
    }
}
